package cn.wit.testalgorithms;

/**
 * 541.正确的反转字符 题目其实很简单，我之前想到误区里面去了 用两个循环，一个循环找位置，一个循环替换
 * 
 * @author 16604
 *
 */

public class DemoTrueSwapAdjacentChar {
	public static void main(String[] args) {
		String s = "abcdefg";
		int k = 5;
//		System.out.println(reverseStr(s, k));
		reverseStr(s, k);
		

	}

	public static String reverseStr(String s, int k) {
		StringBuilder sb = new StringBuilder(s);

		sb.reverse();
		System.out.println(sb);
		
		for (int left = 0; left < s.length(); left += 2) { // 第一个循环找替换位置
			for (int i = left, j = Math.min(left + k - 1, (int) s.length() - 1); i < j; ++i, --j) {
			//	System.out.println(i+" "+j+" ");
				char temp = sb.charAt(i);
				sb.setCharAt(i, s.charAt(j));
				sb.setCharAt(j, temp);
			}
		}
		
		s = sb.toString().toLowerCase();
		return s;
	}

}
